#pragma once
#include "lib_MonitorCpu.h"
#include "lib_MonitorMem.h"
#include <sys/types.h>
#include <vector>
#include <string>
#include <stdint.h>
#include <sys/time.h>

#define MAX_PROCESS_NUM 4096
#define BUF_64 64
#define BUF_256 256

typedef struct {
    uint64_t read_bytes;
    uint64_t write_bytes;
    struct timespec ts;
    // 计算值
    float read_speed;
    float write_speed;
}ProcessIO_t;

typedef struct processInfo_ {
    pid_t pid;
    char name[BUF_64];
    char cmdline[BUF_256 - 1];
    char state;
    unsigned long long utime;
    unsigned long long stime;
    unsigned long long starttime;
    unsigned long long vsize;
    unsigned long long rss;
    unsigned long long policy;
    unsigned long long nice;
    ProcessIO_t io;
    // 计算值
    float cpu_usage;
    float mem_usage;
}processInfo_t;

typedef struct allProcessInfo_ {
    unsigned long long read_count;
    unsigned long long cpu_time;
    uint32_t proc_number;
    std::vector<processInfo_t> process_list;
}allProcessInfo_t;

class MonitorProcess
{
private:
    allProcessInfo_t allProcess_[2];
    allProcessInfo_t sort_proc_;
    MonitorCpu* monitorCpu_impl_;
    MonitorMem* monitorMem_impl_;
    long cpu_number_;

public:
    MonitorProcess(MonitorCpu* monitorCpu_impl, MonitorMem* monitorMem_impl);
    ~MonitorProcess();
    allProcessInfo_t* update_allProcCpuTime(bool cal_cpu_usage = true);
    float cal_process_cpuUsage(processInfo_t& prev, processInfo_t& curr, unsigned long long dt_cpu_time, allProcessInfo_t* prev_all_proc);
    void show_allProcessInfo(allProcessInfo_t* all_proc, uint32_t num = 0);
    processInfo_t* find_processInfo_by_pid(pid_t pid, allProcessInfo_t* all_proc);
    allProcessInfo_t* sort_proc_by_cpuUsage(allProcessInfo_t* all_proc);
    ProcessIO_t* get_proc_io(pid_t pid, ProcessIO_t* io);
    float cal_proc_io_speed(ProcessIO_t* prev, ProcessIO_t* curr);
    allProcessInfo_t filter_proc_by_name(allProcessInfo_t* all_proc, std::vector<std::string>& filter_name);
};
